/*
 * FormLogIn.java
 *
 * Created on 12 Maret 2008, 19:26
 */

package edu.unindra.elearning.form.addin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import edu.unindra.elearning.db.ResourceDatabase;
import edu.unindra.elearning.util.PanelTranparan;
import edu.unindra.elearning.util.ViewPortGlass;
import usu.widget.ButtonGlass;
import usu.widget.glass.TextBoxGlass;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author usu
 */
public class ElearningFormSubMainMateriSoalReport extends JPanel implements
		ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 815072320793043258L;
	private PanelTranparan panelEfect;
	private JScrollPane jsp;

	private ViewPortGlass viewPortGlass;
	private JTable table;

	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;

	private TextBoxGlass txtNama;
	private TextBoxGlass txtNoId;
	private TextBoxGlass txtKls;

	private ButtonGlass btnOk;

	private String kls;
	private String mtr;

	private double score;
	private int countTrue;
	private int countSoal;
	private int countFalse;

	private DefaultTableModel model;

	private DateFormat dateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
	private Date dateTime = new Date();
	private Date date = new Date();

	/** Creates new form ElearningFormSubMainMateriSoal */
	public ElearningFormSubMainMateriSoalReport() {
		initComponents();
	}

	public void initComponents() {

		setOpaque(false);
		setLayout(new java.awt.GridBagLayout());

		panelEfect = new PanelTranparan();
		panelEfect.setToolTipText("E'Learning");
		jsp = new JScrollPane();

		viewPortGlass = new ViewPortGlass();

		model = new DefaultTableModel();

		table = new JTable(model);
		model.addColumn("Tipe Materi");
		model.addColumn("Tipe Kelas");
		model.addColumn("Jumlah Soal");
		model.addColumn("Jumlah Yang Benar");
		model.addColumn("Jumlah Yang Salah");
		model.addColumn("Total Score");

		table.setAutoCreateRowSorter(true);
		table.setForeground(new java.awt.Color(255, 255, 255));
		table.setToolTipText("Tabel Score");
		table.setFillsViewportHeight(true);
		table.setSelectionBackground(new java.awt.Color(51, 255, 52));
		table.setSelectionForeground(new java.awt.Color(0, 0, 0));
		table.setOpaque(false);

		viewPortGlass.setView(table);

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));
		jLabel1.setText("Nama");

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel2.setForeground(new java.awt.Color(255, 255, 255));
		jLabel2.setText("Nomor Induk");

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		jLabel3.setForeground(new java.awt.Color(255, 255, 255));
		jLabel3.setText("Kelas");

		txtNama = new TextBoxGlass();
		txtNoId = new TextBoxGlass();
		txtKls = new TextBoxGlass();

		txtNama.setForeground(new java.awt.Color(255, 255, 255));
		txtNama.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtNama.setToolTipText("Masukkan Nama Siswa");
		txtNama.setFocusAccelerator('I');
		txtNama.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtNoId.setForeground(new java.awt.Color(255, 255, 255));
		txtNoId.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtNoId.setToolTipText("Masukkan Nomor Induk Siswa");
		txtNoId.setFocusAccelerator('I');
		txtNoId.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtKls.setForeground(new java.awt.Color(255, 255, 255));
		txtKls.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtKls.setToolTipText("Masukkan Kelas Siswa");
		txtKls.setFocusAccelerator('I');
		txtKls.setFont(new java.awt.Font("Tahoma", 1, 11));

		btnOk = new ButtonGlass();

		btnOk.setText("Simpan");
		btnOk.setToolTipText("Klik ini untuk menyimpan data");
		btnOk.setRoundRect(true);
		btnOk.addActionListener(this);

		jsp.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		jsp.setOpaque(false);
		jsp.setViewport(viewPortGlass);

		panelEfect.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		panelEfect.setRound(false);

		javax.swing.GroupLayout panelEfectLayout = new javax.swing.GroupLayout(
				panelEfect);
		panelEfectLayout
				.setHorizontalGroup(panelEfectLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelEfectLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																Alignment.LEADING,
																panelEfectLayout
																		.createParallelGroup(
																				Alignment.LEADING,
																				false)
																		.addComponent(
																				jLabel1,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				jLabel2,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				jLabel3,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																txtNama,
																GroupLayout.DEFAULT_SIZE,
																467,
																Short.MAX_VALUE)
														.addComponent(
																txtNoId,
																GroupLayout.DEFAULT_SIZE,
																467,
																Short.MAX_VALUE)
														.addComponent(
																txtKls,
																GroupLayout.DEFAULT_SIZE,
																467,
																Short.MAX_VALUE))));
		panelEfectLayout
				.setVerticalGroup(panelEfectLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelEfectLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txtNama,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txtNoId,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel2))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txtKls,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel3))));
		panelEfect.setLayout(panelEfectLayout);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);

		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addComponent(
														panelEfect,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jsp,
														GroupLayout.Alignment.TRAILING,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btnOk,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));

		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(jsp, GroupLayout.DEFAULT_SIZE,
										204, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panelEfect,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														btnOk,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));

	}

	public void initializedata() throws IOException {

		if (model.getRowCount() > 0) {
			model.removeRow(0);
		}

		// Create the first row
		int countFalse = getCountSoal() - getCountTrue();
		model.insertRow(
				0,
				new Object[] { getMtr(), getKls(),
						String.valueOf(getCountSoal()),
						String.valueOf(getCountTrue()),
						String.valueOf(countFalse), String.valueOf(getScore()) });

	}

	@SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent e) {
		@SuppressWarnings("rawtypes")
		List list = new LinkedList();

		list.add(dateTimeFormat.format(dateTime));
		list.add(getMtr());
		list.add(getKls());
		list.add(txtNama.getText());
		list.add(txtNoId.getText());
		list.add(txtKls.getText());
		list.add(dateFormat.format(date));
		list.add(getScore());

		int retVal = 0;
		try {
			retVal = ResourceDatabase.getInstance().insertReports(list);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		if (retVal > -1) {
			JOptionPane.showMessageDialog(this,
					"Terima kasih telah menggunakan E'learning System",
					"Information", JOptionPane.INFORMATION_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(this, "Process Input Data Problem",
					"Error", JOptionPane.ERROR_MESSAGE);
		}

	}

	public int getCountTrue() {
		return countTrue;
	}

	public void setCountTrue(int countTrue) {
		this.countTrue = countTrue;
	}

	public int getCountSoal() {
		return countSoal;
	}

	public void setCountSoal(int countSoal) {
		this.countSoal = countSoal;
	}

	public int getCountFalse() {
		return countFalse;
	}

	public void setCountFalse(int countFalse) {
		this.countFalse = countFalse;
	}

	public void setKls(String kls) {
		this.kls = kls;
	}

	public void setMtr(String mtr) {
		this.mtr = mtr;
	}

	public String getKls() {
		return kls;
	}

	public String getMtr() {
		return mtr;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}
}
